@layout('layouts.system')

@section('content')
<div class="page-header">
    <small>
      {{ HTML::link('administrator/organization/department', Str::upper('SEMUA JABATAN'));}} <strong>/</strong>
      {{ HTML::link('administrator/organization/division/'.URI::segment(5), $smallheader );}}
    </small>
    <h3>{{ $header }}</h3>
</div>

<div class="row-fluid">
	<a href="#addUnitModal" role="button" class="btn pull-right" data-toggle="modal" style='margin-bottom:10px'><i class="icon-plus"></i>&nbsp;{{ Str::title(Lang::line('admin.addunit')->get()) }}</a>
</div>
<div class="row-fluid">
	<div class="span12">
		<div id="listUnit" class="row-fluid">
			{{ $listUnit  }}
		</div>
	</div>
</div>

<!-- Modal -->
<div id="addUnitModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
  <h3 id="myModalLabel">{{ Str::title(Lang::line('admin.addunit')->get()) }}</h3>
</div>
<div class="modal-body">
{{ Form::open('#', 'POST', array('id' => 'addUnitForm', 'class' => 'form-horizontal')) }}
{{ Form::hidden('wpu_no_auto',NULL)}}
{{ Form::hidden('wpb_no_auto',$wpb_no_auto)}}
{{ Form::hidden('wpj_no_auto', $wpj_no_auto)}}
{{ Form::control_group(Form::label('wpu_unit_name', Str::title(Lang::line('admin.unit')->get())),Form::large_text('wpu_unit_name',null,array('placeholder'=>Str::title(Lang::line('global.type')->get().' '.Lang::line('admin.unit')->get()),'required')),'',Form::inline_help('&nbsp;<span class="text-error"><strong>*</strong><small>'.__('global.required')->get().'</small></span>')) }}
{{ Form::control_group(Form::label('wpu_unit_desc', Str::title(Lang::line('global.desc')->get())),Form::large_text('wpu_unit_desc', '', array('placeholder'=>Str::title(Lang::line('global.type')->get().' '.Lang::line('global.desc')->get())))) }}
{{ Form::close()}}
</div>
<div class="modal-footer">
  <button class="btn" data-dismiss="modal" aria-hidden="true">{{ Str::title(Lang::line('global.close')->get()) }}</button>
  <button id="addUnitBtn" class="btn btn-primary">{{ Str::title(Lang::line('global.save')->get()) }}</button>
</div>
</div>

<div id="editUnitModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
  <h3 id="myModalLabel">{{ Str::title(Lang::line('admin.editunit')->get()) }}</h3>
</div>
<div class="modal-body">
{{ Form::open('#', 'POST', array('id' => 'editUnitForm', 'class' => 'form-horizontal')) }}
{{ Form::hidden('wpu_no_auto',NULL)}}
{{ Form::hidden('wpb_no_auto',$wpb_no_auto)}}
{{ Form::hidden('wpj_no_auto', $wpj_no_auto)}}
{{ Form::control_group(Form::label('wpu_unit_name', Str::title(Lang::line('admin.unit')->get())),Form::large_text('wpu_unit_name',null,array('placeholder'=>Str::title(Lang::line('global.type')->get().' '.Lang::line('admin.unit')->get()),'required')),'',Form::inline_help('&nbsp;<span class="text-error"><strong>*</strong><small>'.__('global.required')->get().'</small></span>')) }}
{{ Form::control_group(Form::label('wpu_unit_desc', Str::title(Lang::line('global.desc')->get())),Form::large_text('wpu_unit_desc', '', array('placeholder'=>Str::title(Lang::line('global.type')->get().' '.Lang::line('global.desc')->get())))) }}
{{ Form::close()}}
</div>
<div class="modal-footer">
  <button class="btn" data-dismiss="modal" aria-hidden="true">{{ Str::title(Lang::line('global.close')->get()) }}</button>
  <button id="editUnitBtn" class="btn btn-primary">{{ Str::title(Lang::line('global.edit')->get()) }}</button>
</div>
</div>
@endsection
@section('scripts')
<script type="text/javascript">

$('#addUnitBtn').click(function() {
  $.post('{{ url("administrator/organization/unit/".$wpj_no_auto."/".$wpb_no_auto)}}', $("#addUnitForm").serialize(),function(data) {
          sourcedata = data;
        }).success(function() { 
          sourcedata = jQuery.parseJSON(sourcedata);
          if(sourcedata.messages){
    		validated("addUnitForm",sourcedata.messages);
          }else{
            $("#listUnit" ).empty().html( sourcedata );
            $("#addUnitForm :input").val('');
            $("#addUnitForm :input[name='wpj_no_auto']").val('{{URI::segment(5)}}');
            $("#addUnitForm :input[name='wpb_no_auto']").val('{{URI::segment(6)}}');
            $('#addUnitModal').modal('hide');
          } 
        });
});

$('#editUnitBtn').click(function() {
  $.post('{{ url("administrator/organization/editUnit/".$wpj_no_auto."/".$wpb_no_auto)}}', $("#editUnitForm").serialize(),function(data) {
          sourcedata = data;
        }).success(function() { 
          sourcedata = jQuery.parseJSON(sourcedata);
          if(sourcedata.messages){
    		validated("editUnitForm",sourcedata.messages);
          }else{
            $("#listUnit" ).empty().html( sourcedata );
			window.location.replace('{{ url("administrator/organization/unit/".$wpj_no_auto."/".$wpb_no_auto)}}');
          } 
        });
});

function editUnit(id){
	$('#editUnitModal').modal('toggle');
	//$('#addUnitModal .modal-header #myModalLabel').empty().text("{{ Str::title(Lang::line('global.edit')->get()) }} {{ Str::title(Lang::line('admin.unit')->get()) }} ");

      $.get('{{ url("administrator/organization/unitInfo") }}', { id: id},function(data,status){
        for (x in data)
        {   
          $('#editUnitForm input[name="'+ x +'"]' ).val(data[x]);

        }
      },"json");
}

function deleteUnit(id,name){
    //var r = confirm("{{ Str::title(Lang::line('global.delete')->get()) }} "+"{{ Str::title(Lang::line('admin.unit')->get()) }} " +name.toUpperCase() + " ?");
    var r = confirm("{{ Lang::line('admin.deleteunit')->get() }} " +name.toUpperCase() + " ?");
	
    if (r==true){
        $.post("{{ url('administrator/organization/deleteUnit') }}", "id="+id+"&name="+name ,function(data) {
              sourcedata = data;
            }).success(function() {
	            sourcedata = jQuery.parseJSON(sourcedata);
	            if(sourcedata.fail){
                	alert(sourcedata.fail);
              	}else{
        			$( "#listUnit" ).empty().append( sourcedata );
        		}
				
				window.location.replace('{{ url("administrator/organization/unit/".$wpj_no_auto."/".$wpb_no_auto)}}');
        });
    }
}

</script>
@endsection